//
//  WBNavigationController.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/6/29.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController, UIGestureRecognizerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //重设手势代理
        interactivePopGestureRecognizer?.delegate = self
    }
    
    // 监听手势开始滑动
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        // 首页不接受手势
//        if childViewControllers.count == 1 {
//            return false
//        }
        return (childViewControllers.count != 1)
    }
    
    @objc private func backClick(){
        print("左侧按钮点击")
        self.popViewController(animated: true)
    }
    
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            // 设置默认值
            var title = "返回"
            // 二级界面
            if childViewControllers.count == 1{
                title = childViewControllers.first?.navigationItem.title ?? ""
                // 隐藏tabbar
                viewController.hidesBottomBarWhenPushed = true
            }
            // 设置左侧按钮-- 导致又滑手势失效
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_back_withtext", title: title, target: self, action: #selector(backClick))
        }
        
        // 监听改方法
        super.pushViewController(viewController, animated: animated)
    }
}
